package serene.microbank.menu;

import java.util.ArrayList;
import java.util.List;

import javax.el.ELContext;
import javax.el.MethodExpression;
import javax.faces.component.html.HtmlCommandLink;
import javax.faces.component.html.HtmlOutputLink;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.richfaces.component.html.HtmlDropDownMenu;
import org.richfaces.component.html.HtmlPanelBar;
import org.richfaces.component.html.HtmlPanelBarItem;

import com.wsmicrobank.user.TransGroup;
import com.wsmicrobank.user.TransList;
import com.wsmicrobank.user.User;

import serene.microbank.util.Language;
import serene.microbank.util.TCVN_UNICODE;

import org.richfaces.component.html.HtmlMenuGroup;
import org.richfaces.component.html.HtmlPanelMenu;
import org.richfaces.component.html.HtmlPanelMenuGroup;
import org.richfaces.component.html.HtmlPanelMenuItem;

public class BeanMenu {
	private org.richfaces.component.html.HtmlPanelMenu pannelmenu = new org.richfaces.component.html.HtmlPanelMenu();
	private int menuwidth;
	public org.richfaces.component.html.HtmlPanelMenu getPannelmenu() {
		return pannelmenu;
	}
	public void setPannelmenu(org.richfaces.component.html.HtmlPanelMenu pannelmenu) {
		this.pannelmenu = pannelmenu;
	}
	public int getMenuwidth() {
		return menuwidth;
	}
	public void setMenuwidth(int menuwidth) {
		this.menuwidth = menuwidth;
	}

	FacesContext context = FacesContext.getCurrentInstance();
	HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
	TCVN_UNICODE tcvn_unicode;
	Language language = new Language();
	String currentLanguage = "";
	
	public BeanMenu(){
		pannelmenu = new org.richfaces.component.html.HtmlPanelMenu();
		int tmpid=1;
		User userLogin = (User)session.getAttribute("userLogin");
		TransGroup[] ArrayTransGroup = null;
		TransList[] ArrayTransList = null;
		List<String>tmpTitleMenu = new ArrayList<String>();
		try{
			if(currentLanguage==null) currentLanguage = "VN";
			if(userLogin!=null){
				ArrayTransGroup = userLogin.getListTransGroup();
				ArrayTransList = userLogin.getListTransList();
				pannelmenu.setMode("ajax");
				pannelmenu.setStyle("width:200px");
				pannelmenu.setIconExpandedGroup("disc");
				pannelmenu.setIconCollapsedGroup("disc");
				pannelmenu.setIconCollapsedTopGroup("chevronUp");
				pannelmenu.setIconGroupTopPosition("right");
				pannelmenu.setIconCollapsedTopGroup("chevronDown");
				
				if(ArrayTransGroup!=null){
					for(int i=0;i<ArrayTransGroup.length;i++){
						HtmlPanelMenuGroup grp_menu = new HtmlPanelMenuGroup();
						if(currentLanguage.equals("EN")){
							tmpTitleMenu.add(tcvn_unicode.stringTCVNtoUNICODE(ArrayTransGroup[i].getDescription_en()));
						}else{
							tmpTitleMenu.add(tcvn_unicode.stringTCVNtoUNICODE(ArrayTransGroup[i].getDescription_vn()));
						}
						grp_menu.setLabel(tmpTitleMenu.get(i));
						for(int j=0;j<ArrayTransList.length;j++){
							HtmlPanelMenuItem item_menu = new HtmlPanelMenuItem();
							//item_menu.setId("ID"+tmpid);
							//item_menu.setLabel("Item"+j);
							//item_menu.setAction(null);
							grp_menu.getChildren().add(item_menu);
							tmpid++;
							
							item_menu.setId(ArrayTransList[j].getTrans_code().toString());
							if(currentLanguage.equals("EN")){
								item_menu.setLabel(tcvn_unicode.stringTCVNtoUNICODE(ArrayTransList[j].getDescription_en()));
							}else{
								item_menu.setLabel(tcvn_unicode.stringTCVNtoUNICODE(ArrayTransList[j].getDescription_vn()));
							}
							item_menu.setRendered(true);
							item_menu.setIcon("disc");
							
							item_menu.addActionListener(new MenuActionListener());
							FacesContext fc = FacesContext.getCurrentInstance();
							ELContext ec = fc.getELContext();
							MethodExpression me = fc.getApplication().getExpressionFactory().createMethodExpression(ec,"#{MenuActionListener.MenuAction}", String.class, new Class[]{});
							item_menu.setActionExpression(me);
							
						}
						
					}
				}
			}			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
